/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.nhc.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.astrob.nhcmap.R;

import zuo.biao.library.base.BaseModel;

//航点自动采集
public class PoiCollectInfo extends BaseModel {

    private static final long serialVersionUID = 1L;

    //航迹设置
    private static String POICOLLECTINFO = "poicollectinfo";

    private static String POICOLLECTSET_SAVEMODE = "poicollectset_savemode";//保存模式
    private static String POICOLLECTSET_AUTO = "poicollectset_auto";//自动保存POI
    private static String POICOLLECTSET_INTERVAL_T = "poicollectset_interval_t";//时间间隔
    private static String POICOLLECTSET_INTERVAL_D = "poicollectset_interval_d";//距离间隔

    //采集方式
    public static String[] SAVEMODE_NAMES = {"快速保存", "手动保存"};
    private int savemode = 1;//保存模式 0-快速保存 1-手动保存（弹出保存activity）

    private int autocollect = 0;//自动采集 0-不自动  1-按时间 2-按距离
    private int intervalmode_t = 0;//时间记录间隔 //
    private int intervalmode_d = 1;//距离记录间隔 //

    public static String[] COLLECTMODE_NAMES = {"时间", "距离"};
    public static String[] INTERVALSTR_TIMES = {"1秒", "5秒", "10秒", "15秒", "30秒", "60秒"};
    public static String[] INTERVALSTR_DISTANCES = {"5米", "10米", "15米", "20米", "50米"};
    public static int[] INTERVAL_TIMES = {1, 5, 10, 15, 30, 60};
    public static int[] INTERVAL_DISTANCES = {5, 10, 15, 20, 50};


    private static PoiCollectInfo instance = new PoiCollectInfo();

    public static PoiCollectInfo getInstance() {
        return instance;
    }

    /**
     * 默认构造方法，JSON等解析时必须要有
     */
    public PoiCollectInfo() {
        //default
    }

    public void save(Context context) {
        if (context == null) {
            return;
        }

        SharedPreferences sp = context.getSharedPreferences(
                POICOLLECTINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(POICOLLECTSET_SAVEMODE, this.savemode);
        editor.putInt(POICOLLECTSET_INTERVAL_T, this.intervalmode_t);
        editor.putInt(POICOLLECTSET_INTERVAL_D, this.intervalmode_d);
        editor.putInt(POICOLLECTSET_AUTO, this.autocollect);
        editor.commit();
    }

    public void load(Context context) {
        if (context == null) {
            return;
        }
        SAVEMODE_NAMES = context.getResources().getStringArray(R.array.poicinfo_savemode_names);
        SharedPreferences sp = context.getSharedPreferences(
                POICOLLECTINFO, Context.MODE_PRIVATE);
        this.savemode = sp.getInt(POICOLLECTSET_SAVEMODE, this.savemode);
        this.intervalmode_t = sp.getInt(POICOLLECTSET_INTERVAL_T, this.intervalmode_t);
        this.intervalmode_d = sp.getInt(POICOLLECTSET_INTERVAL_D, this.intervalmode_d);
        this.autocollect = sp.getInt(POICOLLECTSET_AUTO, this.autocollect);
    }

    public void reset() {
        savemode = 1;//保存模式
        intervalmode_t = 0;//时间记录间隔 //
        intervalmode_d = 1;//距离记录间隔 //
        autocollect = 0;//自动保存POI
    }

    public String getSavemodeStr() {
        if (savemode < 0 || savemode >= SAVEMODE_NAMES.length) {
            savemode = 0;
        }
        return SAVEMODE_NAMES[savemode];
    }

    public int getSavemode() {
        return savemode;
    }

    public void setSavemode(int savemode) {
        this.savemode = savemode;
    }

    public int getIntervalmode_t() {
        if (intervalmode_t < 0 || intervalmode_t >= INTERVALSTR_TIMES.length) {
            intervalmode_t = 1;
        }
        return INTERVAL_TIMES[intervalmode_t];
    }

    public void setIntervalmode_t(int intervalmode_t) {
        this.intervalmode_t = intervalmode_t;
    }

    public int getIntervalmode_d() {
        if (intervalmode_d < 0 || intervalmode_d >= INTERVALSTR_DISTANCES.length) {
            intervalmode_d = 1;
        }
        return INTERVAL_DISTANCES[intervalmode_d];
    }

    public void setIntervalmode_d(int intervalmode_d) {
        this.intervalmode_d = intervalmode_d;
    }

    public int getAutocollect() {
        return autocollect;
    }

    public void setAutocollect(int autocollect) {
        this.autocollect = autocollect;
    }

    public String getCollectmodeStr() {
        if (autocollect == 2) {
            return COLLECTMODE_NAMES[1];
        }
        return COLLECTMODE_NAMES[0];
    }

    public String getIntervalStr() {
        if (autocollect == 1) {
            if (intervalmode_t < 0 || intervalmode_t >= INTERVALSTR_TIMES.length) {
                intervalmode_t = 1;
            }
            return INTERVALSTR_TIMES[intervalmode_t];
        } else {
            if (intervalmode_d < 0 || intervalmode_d >= INTERVALSTR_DISTANCES.length) {
                intervalmode_d = 1;
            }
            return INTERVALSTR_DISTANCES[intervalmode_d];
        }
    }

    public void setIntervalmode(int intervalmode) {
        if (autocollect == 1) {
            this.intervalmode_t = intervalmode;
        } else {
            this.intervalmode_d = intervalmode;
        }
    }

    @Override
    protected boolean isCorrect() {//根据自己的需求决定，也可以直接 return true
        return id > 0;// && StringUtil.isNotEmpty(phone, true);
    }

}
